﻿namespace WMS6.SQLServerDAL
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Common;
    using System.Data.SqlClient;
    using System.Text;
    using WMS6.Common;
    using WMS6.DataEntity;
    using WMS6.DBUtility;
    using WMS6.IDAL;
    using WMS6.QueryEntity;

    public class XpickDetailDA : IXpickDetailDA
    {
        private string ALTSKU = "@ALTSKU";
        private string CARTON_GROUP = "@CARTON_GROUP";
        private string CARTON_TYPE = "@CARTON_TYPE";
        private string CASE_ID = "@CASE_ID";
        private string CREATED_BY = "@CREATED_BY";
        private string DO_CARTONIZE = "@DO_CARTONIZE";
        private string DO_REPLENISH = "@DO_REPLENISH";
        private string DOOR = "@DOOR";
        private string DROP_ID = "@DROP_ID";
        private string EFFECTIVE_DATE = "@EFFECTIVE_DATE";
        private string LOAD_ID = "@LOAD_ID";
        private string LOC = "@LOC";
        private string LOT = "@LOT";
        private string LPN_ID = "@LPN_ID";
        private string MOVED_QTY = "@MOVED_QTY";
        private string OPTIMIZE_COP = "@OPTIMIZE_COP";
        private string ORDER_ID = "@ORDER_ID";
        private string ORDER_LINE_ID = "@ORDER_LINE_ID";
        private string OWNER_ID = "@OWNER_ID";
        private string PACK_ID = "@PACK_ID";
        private string PICK_DETAIL_ID = "@PICK_DETAIL_ID";
        private string PICK_HEADER_ID = "@PICK_HEADER_ID";
        private string PICK_METHOD = "@PICK_METHOD";
        private string QTY = "@QTY";
        private string REFID = "@REFID";
        private string REPLENISH_ZONE = "@REPLENISH_ZONE";
        private string ROUTE = "@ROUTE";
        private string SKU_ID = "@SKU_ID";
        private string SQL_CHECK_XPICK_DETAIL_ID_UNIQUE = " SELECT COUNT(1) FROM @_@XPICK_DETAIL WHERE PICK_DETAIL_ID = @PICK_DETAIL_ID ";
        private string SQL_DELETE_XPICK_DETAIL = " DELETE FROM @_@XPICK_DETAIL WHERE PICK_DETAIL_ID = @PICK_DETAIL_ID ";
        private string SQL_INSERT_XPICK_DETAIL = " INSERT INTO @_@XPICK_DETAIL ( WH_ID, PICK_DETAIL_ID, CASE_ID, PICK_HEADER_ID, ORDER_ID, ORDER_LINE_ID, LOT, OWNER_ID, SKU_ID, ALTSKU, UOM_ID, UOM_QTY, QTY, MOVED_QTY, STATUS, DROP_ID, LOC, LPN_ID, REFID, PACK_ID, UPDATE_SOURCE, CARTON_GROUP, CARTON_TYPE, TO_LOC, DO_REPLENISH, REPLENISH_ZONE, DO_CARTONIZE, PICK_METHOD, WAVE_ID, EFFECTIVE_DATE, CREATED_BY, CREATED_DATE, UPDATED_BY, UPDATED_DATE, OPTIMIZE_COP, LOAD_ID, STOP, DOOR, ROUTE) VALUES ( @WH_ID, @PICK_DETAIL_ID, @CASE_ID, @PICK_HEADER_ID, @ORDER_ID, @ORDER_LINE_ID, @LOT, @OWNER_ID, @SKU_ID, @ALTSKU, @UOM_ID, @UOM_QTY, @QTY, @MOVED_QTY, @STATUS, @DROP_ID, @LOC, @LPN_ID, @REFID, @PACK_ID, @UPDATE_SOURCE, @CARTON_GROUP, @CARTON_TYPE, @TO_LOC, @DO_REPLENISH, @REPLENISH_ZONE, @DO_CARTONIZE, @PICK_METHOD, @WAVE_ID, @EFFECTIVE_DATE, @CREATED_BY, GETDATE(), @UPDATED_BY, GETDATE(), @OPTIMIZE_COP, @LOAD_ID, @STOP, @DOOR, @ROUTE)  ";
        private string SQL_SELECT_ALL_XPICK_DETAIL = " SELECT  ROWID, WH_ID, PICK_DETAIL_ID, CASE_ID, PICK_HEADER_ID, ORDER_ID, ORDER_LINE_ID, LOT, OWNER_ID, SKU_ID, ALTSKU, UOM_ID, UOM_QTY, QTY, MOVED_QTY, STATUS, DROP_ID, LOC, LPN_ID, REFID, PACK_ID, UPDATE_SOURCE, CARTON_GROUP, CARTON_TYPE, TO_LOC, DO_REPLENISH, REPLENISH_ZONE, DO_CARTONIZE, PICK_METHOD, WAVE_ID, EFFECTIVE_DATE, CREATED_BY, CREATED_DATE, UPDATED_BY, UPDATED_DATE, OPTIMIZE_COP, LOAD_ID, STOP, DOOR, ROUTE  FROM  @_@XPICK_DETAIL ";
        private string SQL_UPDATE_XPICK_DETAIL = " UPDATE @_@XPICK_DETAIL SET WH_ID = @WH_ID, CASE_ID = @CASE_ID, PICK_HEADER_ID = @PICK_HEADER_ID, ORDER_ID = @ORDER_ID, ORDER_LINE_ID = @ORDER_LINE_ID, LOT = @LOT, OWNER_ID = @OWNER_ID, SKU_ID = @SKU_ID, ALTSKU = @ALTSKU, UOM_ID = @UOM_ID, UOM_QTY = @UOM_QTY, QTY = @QTY, MOVED_QTY = @MOVED_QTY, STATUS = @STATUS, DROP_ID = @DROP_ID, LOC = @LOC, LPN_ID = @LPN_ID, REFID = @REFID, PACK_ID = @PACK_ID, UPDATE_SOURCE = @UPDATE_SOURCE, CARTON_GROUP = @CARTON_GROUP, CARTON_TYPE = @CARTON_TYPE, TO_LOC = @TO_LOC, DO_REPLENISH = @DO_REPLENISH, REPLENISH_ZONE = @REPLENISH_ZONE, DO_CARTONIZE = @DO_CARTONIZE, PICK_METHOD = @PICK_METHOD, WAVE_ID = @WAVE_ID, EFFECTIVE_DATE = @EFFECTIVE_DATE, UPDATED_BY = @UPDATED_BY, UPDATED_DATE = GETDATE(), OPTIMIZE_COP = @OPTIMIZE_COP, LOAD_ID = @LOAD_ID, STOP = @STOP, DOOR = @DOOR, ROUTE = @ROUTE WHERE PICK_DETAIL_ID = @PICK_DETAIL_ID  ";
        private string STATUS = "@STATUS";
        private string STOP = "@STOP";
        private string TO_LOC = "@TO_LOC";
        private string UOM_ID = "@UOM_ID";
        private string UOM_QTY = "@UOM_QTY";
        private string UPDATE_SOURCE = "@UPDATE_SOURCE";
        private string UPDATED_BY = "@UPDATED_BY";
        private string WAVE_ID = "@WAVE_ID";
        private string WH_ID = "@WH_ID";

        public bool CheckXpickDetailIDUnique(string xpickDetailID, string whLoginID)
        {
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@PICK_DETAIL_ID", xpickDetailID) };
            return (DBHelper.ExecuteScalar(CommandType.Text, this.SQL_CHECK_XPICK_DETAIL_ID_UNIQUE.Replace("@_@", whLoginID), paras).ToString() == "1");
        }

        public bool CheckXpickDetailIDUnique(DataBase dataBase, DbTransaction tran, string xpickDetailID, string whLoginID)
        {
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@PICK_DETAIL_ID", xpickDetailID) };
            return (dataBase.ExecuteScalar(tran, CommandType.Text, this.SQL_CHECK_XPICK_DETAIL_ID_UNIQUE.Replace("@_@", whLoginID), paras).ToString() == "1");
        }

        public int DeleteXpickDetail(List<string> xpickDetailID, string whLoginID)
        {
            int result = 0;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                for (int j = 0; j < xpickDetailID.Count; j++)
                {
                    SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@PICK_DETAIL_ID", xpickDetailID[j]) };
                    result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_DELETE_XPICK_DETAIL.Replace("@_@", whLoginID), paras);
                }
                tran.Commit();
            }
            catch (SqlException sqlEx)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return result;
        }

        public int DeleteXpickDetail(DataBase dataBase, DbTransaction tran, List<string> xpickDetailID, string whLoginID)
        {
            int result = 0;
            try
            {
                for (int j = 0; j < xpickDetailID.Count; j++)
                {
                    SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@PICK_DETAIL_ID", xpickDetailID[j]) };
                    result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_DELETE_XPICK_DETAIL.Replace("@_@", whLoginID), paras);
                }
            }
            catch (SqlException sqlEx)
            {
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }

        public DataSet GetAllXpickDetail(string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_XPICK_DETAIL.Replace("@_@", whLoginID);
            return DBHelper.ExecuteDataSet(CommandType.Text, sql);
        }

        public DataSet GetAllXpickDetail(DataBase dataBase, DbTransaction tran, string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_XPICK_DETAIL.Replace("@_@", whLoginID);
            return dataBase.ExecuteDataSet(tran, CommandType.Text, sql);
        }

        public XpickDetailInfo GetXpickDetailByID(string xpickdetailID, string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_XPICK_DETAIL.Replace("@_@", whLoginID) + " WHERE PICK_DETAIL_ID = @PICK_DETAIL_ID  ";
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@PICK_DETAIL_ID", xpickdetailID) };
            XpickDetailInfo xpickDetailInfo = null;
            using (IDataReader reader = DBHelper.ExecuteReader(CommandType.Text, sql, paras))
            {
                if (reader.Read())
                {
                    DateTime CS00003;
                    xpickDetailInfo = new XpickDetailInfo((reader["ROWID"].ToString() != "") ? int.Parse(reader["ROWID"].ToString()) : 0, reader["WH_ID"].ToString(), reader["PICK_DETAIL_ID"].ToString(), reader["CASE_ID"].ToString(), reader["PICK_HEADER_ID"].ToString(), reader["ORDER_ID"].ToString(), (reader["ORDER_LINE_ID"].ToString() != "") ? int.Parse(reader["ORDER_LINE_ID"].ToString()) : 0, reader["LOT"].ToString(), reader["OWNER_ID"].ToString(), reader["SKU_ID"].ToString(), reader["ALTSKU"].ToString(), reader["UOM_ID"].ToString(), (reader["UOM_QTY"].ToString() != "") ? decimal.Parse(reader["UOM_QTY"].ToString()) : 0M, (reader["QTY"].ToString() != "") ? decimal.Parse(reader["QTY"].ToString()) : 0M, (reader["MOVED_QTY"].ToString() != "") ? decimal.Parse(reader["MOVED_QTY"].ToString()) : 0M, reader["STATUS"].ToString(), reader["DROP_ID"].ToString(), reader["LOC"].ToString(), reader["LPN_ID"].ToString(), reader["REFID"].ToString(), reader["PACK_ID"].ToString(), reader["UPDATE_SOURCE"].ToString(), reader["CARTON_GROUP"].ToString(), reader["CARTON_TYPE"].ToString(), reader["TO_LOC"].ToString(), reader["DO_REPLENISH"].ToString(), reader["REPLENISH_ZONE"].ToString(), reader["DO_CARTONIZE"].ToString(), reader["PICK_METHOD"].ToString(), reader["WAVE_ID"].ToString(), (reader["EFFECTIVE_DATE"].ToString() != "") ? DateTime.Parse(reader["EFFECTIVE_DATE"].ToString()) : (CS00003 = new DateTime()), reader["CREATED_BY"].ToString(), (reader["CREATED_DATE"].ToString() != "") ? DateTime.Parse(reader["CREATED_DATE"].ToString()) : (CS00003 = new DateTime()), reader["UPDATED_BY"].ToString(), (reader["UPDATED_DATE"].ToString() != "") ? DateTime.Parse(reader["UPDATED_DATE"].ToString()) : new DateTime(), reader["OPTIMIZE_COP"].ToString(), reader["LOAD_ID"].ToString(), (reader["STOP"].ToString() != "") ? int.Parse(reader["STOP"].ToString()) : 0, reader["DOOR"].ToString(), reader["ROUTE"].ToString());
                }
            }
            return xpickDetailInfo;
        }

        public XpickDetailInfo GetXpickDetailByID(DataBase dataBase, DbTransaction tran, string xpickdetailID, string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_XPICK_DETAIL.Replace("@_@", whLoginID) + " WHERE PICK_DETAIL_ID = @PICK_DETAIL_ID  ";
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@PICK_DETAIL_ID", xpickdetailID) };
            XpickDetailInfo xpickDetailInfo = null;
            IDataReader reader = dataBase.ExecuteReader(tran, CommandType.Text, sql, paras);
            if (reader.Read())
            {
                DateTime CS00003;
                xpickDetailInfo = new XpickDetailInfo((reader["ROWID"].ToString() != "") ? int.Parse(reader["ROWID"].ToString()) : 0, reader["WH_ID"].ToString(), reader["PICK_DETAIL_ID"].ToString(), reader["CASE_ID"].ToString(), reader["PICK_HEADER_ID"].ToString(), reader["ORDER_ID"].ToString(), (reader["ORDER_LINE_ID"].ToString() != "") ? int.Parse(reader["ORDER_LINE_ID"].ToString()) : 0, reader["LOT"].ToString(), reader["OWNER_ID"].ToString(), reader["SKU_ID"].ToString(), reader["ALTSKU"].ToString(), reader["UOM_ID"].ToString(), (reader["UOM_QTY"].ToString() != "") ? decimal.Parse(reader["UOM_QTY"].ToString()) : 0M, (reader["QTY"].ToString() != "") ? decimal.Parse(reader["QTY"].ToString()) : 0M, (reader["MOVED_QTY"].ToString() != "") ? decimal.Parse(reader["MOVED_QTY"].ToString()) : 0M, reader["STATUS"].ToString(), reader["DROP_ID"].ToString(), reader["LOC"].ToString(), reader["LPN_ID"].ToString(), reader["REFID"].ToString(), reader["PACK_ID"].ToString(), reader["UPDATE_SOURCE"].ToString(), reader["CARTON_GROUP"].ToString(), reader["CARTON_TYPE"].ToString(), reader["TO_LOC"].ToString(), reader["DO_REPLENISH"].ToString(), reader["REPLENISH_ZONE"].ToString(), reader["DO_CARTONIZE"].ToString(), reader["PICK_METHOD"].ToString(), reader["WAVE_ID"].ToString(), (reader["EFFECTIVE_DATE"].ToString() != "") ? DateTime.Parse(reader["EFFECTIVE_DATE"].ToString()) : (CS00003 = new DateTime()), reader["CREATED_BY"].ToString(), (reader["CREATED_DATE"].ToString() != "") ? DateTime.Parse(reader["CREATED_DATE"].ToString()) : (CS00003 = new DateTime()), reader["UPDATED_BY"].ToString(), (reader["UPDATED_DATE"].ToString() != "") ? DateTime.Parse(reader["UPDATED_DATE"].ToString()) : new DateTime(), reader["OPTIMIZE_COP"].ToString(), reader["LOAD_ID"].ToString(), (reader["STOP"].ToString() != "") ? int.Parse(reader["STOP"].ToString()) : 0, reader["DOOR"].ToString(), reader["ROUTE"].ToString());
            }
            if (!reader.IsClosed)
            {
                reader.Close();
            }
            return xpickDetailInfo;
        }

        public DataSet GetXpickDetailByQueryList(List<string> sqlWhere, XpickDetailQueryEntity xpickdetailQuery, string whLoginID)
        {
            string temp = this.SQL_SELECT_ALL_XPICK_DETAIL.Replace("@_@", whLoginID);
            for (int i = 0; i < sqlWhere.Count; i++)
            {
                if (i == 0)
                {
                    temp = temp + " WHERE " + sqlWhere[i].ToString();
                }
                else
                {
                    temp = temp + " AND " + sqlWhere[i].ToString();
                }
            }
            if (!xpickdetailQuery.IsGetAll)
            {
                temp = PagingHelper.GetPagingSQL(temp, xpickdetailQuery.CurrentPage, xpickdetailQuery.PageSize, xpickdetailQuery.SortField, xpickdetailQuery.SortDirection);
            }
            return DBHelper.ExecuteDataSet(CommandType.Text, temp);
        }

        public DataSet GetXpickDetailByQueryList(DataBase dataBase, DbTransaction tran, List<string> sqlWhere, XpickDetailQueryEntity xpickdetailQuery, string whLoginID)
        {
            string temp = this.SQL_SELECT_ALL_XPICK_DETAIL.Replace("@_@", whLoginID);
            for (int i = 0; i < sqlWhere.Count; i++)
            {
                if (i == 0)
                {
                    temp = temp + " WHERE " + sqlWhere[i].ToString();
                }
                else
                {
                    temp = temp + " AND " + sqlWhere[i].ToString();
                }
            }
            temp = PagingHelper.GetPagingSQL(temp, xpickdetailQuery.CurrentPage, xpickdetailQuery.PageSize, xpickdetailQuery.SortField, xpickdetailQuery.SortDirection);
            return dataBase.ExecuteDataSet(tran, CommandType.Text, temp);
        }

        public List<XpickDetailInfo> GetXpickDetailListByID(string xorderID, string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_XPICK_DETAIL.Replace("@_@", whLoginID) + " WHERE ORDER_ID = @ORDER_ID  ";
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@ORDER_ID", xorderID) };
            List<XpickDetailInfo> xpickDetailList = new List<XpickDetailInfo>();
            using (IDataReader reader = DBHelper.ExecuteReader(CommandType.Text, sql, paras))
            {
                while (reader.Read())
                {
                    DateTime CS00002;
                    XpickDetailInfo xpickDetailInfo = new XpickDetailInfo((reader["ROWID"].ToString() != "") ? int.Parse(reader["ROWID"].ToString()) : 0, reader["WH_ID"].ToString(), reader["PICK_DETAIL_ID"].ToString(), reader["CASE_ID"].ToString(), reader["PICK_HEADER_ID"].ToString(), reader["ORDER_ID"].ToString(), (reader["ORDER_LINE_ID"].ToString() != "") ? int.Parse(reader["ORDER_LINE_ID"].ToString()) : 0, reader["LOT"].ToString(), reader["OWNER_ID"].ToString(), reader["SKU_ID"].ToString(), reader["ALTSKU"].ToString(), reader["UOM_ID"].ToString(), (reader["UOM_QTY"].ToString() != "") ? decimal.Parse(reader["UOM_QTY"].ToString()) : 0M, (reader["QTY"].ToString() != "") ? decimal.Parse(reader["QTY"].ToString()) : 0M, (reader["MOVED_QTY"].ToString() != "") ? decimal.Parse(reader["MOVED_QTY"].ToString()) : 0M, reader["STATUS"].ToString(), reader["DROP_ID"].ToString(), reader["LOC"].ToString(), reader["LPN_ID"].ToString(), reader["REFID"].ToString(), reader["PACK_ID"].ToString(), reader["UPDATE_SOURCE"].ToString(), reader["CARTON_GROUP"].ToString(), reader["CARTON_TYPE"].ToString(), reader["TO_LOC"].ToString(), reader["DO_REPLENISH"].ToString(), reader["REPLENISH_ZONE"].ToString(), reader["DO_CARTONIZE"].ToString(), reader["PICK_METHOD"].ToString(), reader["WAVE_ID"].ToString(), (reader["EFFECTIVE_DATE"].ToString() != "") ? DateTime.Parse(reader["EFFECTIVE_DATE"].ToString()) : (CS00002 = new DateTime()), reader["CREATED_BY"].ToString(), (reader["CREATED_DATE"].ToString() != "") ? DateTime.Parse(reader["CREATED_DATE"].ToString()) : (CS00002 = new DateTime()), reader["UPDATED_BY"].ToString(), (reader["UPDATED_DATE"].ToString() != "") ? DateTime.Parse(reader["UPDATED_DATE"].ToString()) : (CS00002 = new DateTime()), reader["OPTIMIZE_COP"].ToString(), reader["LOAD_ID"].ToString(), (reader["STOP"].ToString() != "") ? int.Parse(reader["STOP"].ToString()) : 0, reader["DOOR"].ToString(), reader["ROUTE"].ToString());
                    xpickDetailList.Add(xpickDetailInfo);
                }
            }
            return xpickDetailList;
        }

        public int InsertXpickDetail(XpickDetailInfo xpickDetailInfo, string whLoginID)
        {
            int result = 0;
            DataBase db = new DataBase();
            DbConnection conn = db.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                xpickDetailInfo.WhID = whLoginID.Substring(0, whLoginID.Length - 1);
                SqlParameter[] paras = this.Set_XpickDetail_Parameters(xpickDetailInfo);
                if (paras != null)
                {
                    result += db.ExecuteNonQuery(tran, CommandType.Text, this.SQL_INSERT_XPICK_DETAIL.Replace("@_@", whLoginID), paras);
                }
                tran.Commit();
            }
            catch (SqlException sqlEx)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return result;
        }

        public int InsertXpickDetail(DataBase dataBase, DbTransaction tran, XpickDetailInfo xpickDetailInfo, string whLoginID)
        {
            int result = 0;
            try
            {
                xpickDetailInfo.WhID = whLoginID;
                SqlParameter[] paras = this.Set_XpickDetail_Parameters(xpickDetailInfo);
                if (paras != null)
                {
                    result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_INSERT_XPICK_DETAIL.Replace("@_@", whLoginID), paras);
                }
            }
            catch (SqlException sqlEx)
            {
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }

        private SqlParameter[] Set_XpickDetail_Parameters(XpickDetailInfo xpickDetailInfo)
        {
            SqlParameter[] paramArray = new SqlParameter[] { 
                new SqlParameter(this.WH_ID, SqlDbType.VarChar, 30), new SqlParameter(this.PICK_DETAIL_ID, SqlDbType.VarChar, 20), new SqlParameter(this.CASE_ID, SqlDbType.VarChar, 20), new SqlParameter(this.PICK_HEADER_ID, SqlDbType.VarChar, 20), new SqlParameter(this.ORDER_ID, SqlDbType.VarChar, 20), new SqlParameter(this.ORDER_LINE_ID, SqlDbType.Int, 4), new SqlParameter(this.LOT, SqlDbType.VarChar, 20), new SqlParameter(this.OWNER_ID, SqlDbType.VarChar, 20), new SqlParameter(this.SKU_ID, SqlDbType.VarChar, 50), new SqlParameter(this.ALTSKU, SqlDbType.VarChar, 50), new SqlParameter(this.UOM_ID, SqlDbType.VarChar, 20), new SqlParameter(this.UOM_QTY, SqlDbType.Decimal, 13), new SqlParameter(this.QTY, SqlDbType.Decimal, 13), new SqlParameter(this.MOVED_QTY, SqlDbType.Decimal, 13), new SqlParameter(this.STATUS, SqlDbType.VarChar, 20), new SqlParameter(this.DROP_ID, SqlDbType.VarChar, 20), 
                new SqlParameter(this.LOC, SqlDbType.VarChar, 20), new SqlParameter(this.LPN_ID, SqlDbType.VarChar, 20), new SqlParameter(this.REFID, SqlDbType.VarChar, 20), new SqlParameter(this.PACK_ID, SqlDbType.VarChar, 50), new SqlParameter(this.UPDATE_SOURCE, SqlDbType.VarChar, 20), new SqlParameter(this.CARTON_GROUP, SqlDbType.VarChar, 20), new SqlParameter(this.CARTON_TYPE, SqlDbType.VarChar, 20), new SqlParameter(this.TO_LOC, SqlDbType.VarChar, 20), new SqlParameter(this.DO_REPLENISH, SqlDbType.VarChar, 1), new SqlParameter(this.REPLENISH_ZONE, SqlDbType.VarChar, 20), new SqlParameter(this.DO_CARTONIZE, SqlDbType.VarChar, 1), new SqlParameter(this.PICK_METHOD, SqlDbType.VarChar, 1), new SqlParameter(this.WAVE_ID, SqlDbType.VarChar, 20), new SqlParameter(this.EFFECTIVE_DATE, SqlDbType.DateTime, 8), new SqlParameter(this.CREATED_BY, SqlDbType.VarChar, 20), new SqlParameter(this.UPDATED_BY, SqlDbType.VarChar, 20), 
                new SqlParameter(this.OPTIMIZE_COP, SqlDbType.VarChar, 1), new SqlParameter(this.LOAD_ID, SqlDbType.VarChar, 20), new SqlParameter(this.STOP, SqlDbType.Int, 4), new SqlParameter(this.DOOR, SqlDbType.VarChar, 20), new SqlParameter(this.ROUTE, SqlDbType.VarChar, 20)
             };
            if (!string.IsNullOrEmpty(xpickDetailInfo.WhID))
            {
                paramArray[0].Value = xpickDetailInfo.WhID;
            }
            else
            {
                paramArray[0].Value = DBNull.Value;
            }
            paramArray[1].Value = xpickDetailInfo.PickDetailID;
            paramArray[2].Value = xpickDetailInfo.CaseID;
            paramArray[3].Value = xpickDetailInfo.PickHeaderID;
            paramArray[4].Value = xpickDetailInfo.OrderID;
            paramArray[5].Value = xpickDetailInfo.OrderLineID;
            paramArray[6].Value = xpickDetailInfo.Lot;
            paramArray[7].Value = xpickDetailInfo.OwnerID;
            paramArray[8].Value = xpickDetailInfo.SkuID;
            paramArray[9].Value = xpickDetailInfo.Altsku;
            paramArray[10].Value = xpickDetailInfo.UomID;
            paramArray[11].Value = xpickDetailInfo.UomQty;
            paramArray[12].Value = xpickDetailInfo.Qty;
            paramArray[13].Value = xpickDetailInfo.MovedQty;
            paramArray[14].Value = xpickDetailInfo.Status;
            paramArray[15].Value = xpickDetailInfo.DropID;
            paramArray[0x10].Value = xpickDetailInfo.Loc;
            paramArray[0x11].Value = xpickDetailInfo.LpnID;
            paramArray[0x12].Value = xpickDetailInfo.Refid;
            paramArray[0x13].Value = xpickDetailInfo.PackID;
            paramArray[20].Value = xpickDetailInfo.UpdateSource;
            if (!string.IsNullOrEmpty(xpickDetailInfo.CartonGroup))
            {
                paramArray[0x15].Value = xpickDetailInfo.CartonGroup;
            }
            else
            {
                paramArray[0x15].Value = DBNull.Value;
            }
            if (!string.IsNullOrEmpty(xpickDetailInfo.CartonType))
            {
                paramArray[0x16].Value = xpickDetailInfo.CartonType;
            }
            else
            {
                paramArray[0x16].Value = DBNull.Value;
            }
            if (!string.IsNullOrEmpty(xpickDetailInfo.ToLoc))
            {
                paramArray[0x17].Value = xpickDetailInfo.ToLoc;
            }
            else
            {
                paramArray[0x17].Value = DBNull.Value;
            }
            if (!string.IsNullOrEmpty(xpickDetailInfo.DoReplenish))
            {
                paramArray[0x18].Value = xpickDetailInfo.DoReplenish;
            }
            else
            {
                paramArray[0x18].Value = DBNull.Value;
            }
            if (!string.IsNullOrEmpty(xpickDetailInfo.ReplenishZone))
            {
                paramArray[0x19].Value = xpickDetailInfo.ReplenishZone;
            }
            else
            {
                paramArray[0x19].Value = DBNull.Value;
            }
            if (!string.IsNullOrEmpty(xpickDetailInfo.DoCartonize))
            {
                paramArray[0x1a].Value = xpickDetailInfo.DoCartonize;
            }
            else
            {
                paramArray[0x1a].Value = DBNull.Value;
            }
            paramArray[0x1b].Value = xpickDetailInfo.PickMethod;
            paramArray[0x1c].Value = xpickDetailInfo.WaveID;
            paramArray[0x1d].Value = xpickDetailInfo.EffectiveDate;
            paramArray[30].Value = xpickDetailInfo.CreatedBy;
            paramArray[0x1f].Value = xpickDetailInfo.UpdatedBy;
            if (!string.IsNullOrEmpty(xpickDetailInfo.OptimizeCop))
            {
                paramArray[0x20].Value = xpickDetailInfo.OptimizeCop;
            }
            else
            {
                paramArray[0x20].Value = DBNull.Value;
            }
            if (!string.IsNullOrEmpty(xpickDetailInfo.LoadID))
            {
                paramArray[0x21].Value = xpickDetailInfo.LoadID;
            }
            else
            {
                paramArray[0x21].Value = DBNull.Value;
            }
            paramArray[0x22].Value = xpickDetailInfo.Stop;
            if (!string.IsNullOrEmpty(xpickDetailInfo.Door))
            {
                paramArray[0x23].Value = xpickDetailInfo.Door;
            }
            else
            {
                paramArray[0x23].Value = DBNull.Value;
            }
            if (!string.IsNullOrEmpty(xpickDetailInfo.Route))
            {
                paramArray[0x24].Value = xpickDetailInfo.Route;
                return paramArray;
            }
            paramArray[0x24].Value = DBNull.Value;
            return paramArray;
        }

        public int UpdateXpickDetail(XpickDetailInfo xpickDetailInfo, string whLoginID)
        {
            int result = 0;
            DataBase db = new DataBase();
            DbConnection conn = db.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                xpickDetailInfo.WhID = whLoginID;
                SqlParameter[] paras = this.Set_XpickDetail_Parameters(xpickDetailInfo);
                if (paras != null)
                {
                    result += db.ExecuteNonQuery(tran, CommandType.Text, this.SQL_UPDATE_XPICK_DETAIL.Replace("@_@", whLoginID), paras);
                }
                tran.Commit();
            }
            catch (SqlException sqlEx)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return result;
        }

        public int UpdateXpickDetail(DataBase dataBase, DbTransaction tran, XpickDetailInfo xpickDetailInfo, string whLoginID)
        {
            int result = 0;
            try
            {
                xpickDetailInfo.WhID = whLoginID;
                SqlParameter[] paras = this.Set_XpickDetail_Parameters(xpickDetailInfo);
                if (paras != null)
                {
                    result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_UPDATE_XPICK_DETAIL.Replace("@_@", whLoginID), paras);
                }
            }
            catch (SqlException sqlEx)
            {
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }

        public int UpdateXpickDetailStatus(DataBase dataBase, DbTransaction tran, string xPickDetailID, string status, string whLoginID, PubParasInfo pubParasInfo)
        {
            int result = 0;
            try
            {
                StringBuilder strSql = new StringBuilder();
                strSql.AppendFormat(" UPDATE {0}PICK_DETAIL SET STATUS='{1}', UPDATED_DATE=GETDATE(), UPDATED_BY='{3}' WHERE PICK_DETAIL_ID={2}", new object[] { whLoginID, status, xPickDetailID, pubParasInfo.UserID });
                result += dataBase.ExecuteNonQuery(tran, CommandType.Text, strSql.ToString());
            }
            catch (SqlException sqlEx)
            {
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }
    }
}

